package com.sky.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sky.domain.PmsAttrGroup;
import com.sky.service.PmsAttrGroupService;
import com.sky.mapper.PmsAttrGroupMapper;
import com.sky.service.PmsAttrService;
import com.sky.utils.PageQuery;
import com.sky.utils.Query;
import com.sky.utils.StringUtils;
import com.sky.vo.request.GroupWithAttrVO;
import com.sky.vo.request.PmsAttrRequestVO;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author Administrator
* @description 针对表【pms_attr_group(属性分组)】的数据库操作Service实现
* @createDate 2024-06-30 16:46:42
*/
@Service
public class PmsAttrGroupServiceImpl extends ServiceImpl<PmsAttrGroupMapper, PmsAttrGroup> implements PmsAttrGroupService{

    @Resource
    @Lazy
    private PmsAttrService pmsAttrService;

    /**
     * 根据分页查询条件查询属性分组信息。
     *
     * @param param 分页查询条件，包含属性分组的查询参数和分页信息。
     * @return 返回分页结果，其中包含PmsAttrGroup类型的列表。
     */
    @Override
    public IPage<PmsAttrGroup> queryPage(PageQuery<PmsAttrGroup> param) {
        LambdaQueryWrapper<PmsAttrGroup> wrapper = new LambdaQueryWrapper<>();
        PmsAttrGroup pmsAttrGroup = param.getQuery();
        if (StringUtils.isNotEmpty(pmsAttrGroup.getCatalogId())){
            wrapper.eq(PmsAttrGroup::getCatalogId, pmsAttrGroup.getCatalogId());
        }
        if (StringUtils.isNotEmpty(pmsAttrGroup.getAttrGroupName())){
            wrapper.like(PmsAttrGroup::getAttrGroupName, pmsAttrGroup.getAttrGroupName());
        }
        IPage<PmsAttrGroup> page = baseMapper.selectPage(new Query<PmsAttrGroup>().getPage(param), wrapper);
        return page;
    }

    /**
     * 根据分类ID获取属性分组及其属性信息。
     *
     * @param catalogId 分类ID，用于查询与该分类关联的属性分组及其属性信息。
     * @return 返回一个包含属性分组及其属性信息的列表。
     */
    @Override
    public List<GroupWithAttrVO> getGroupWithAttrByCategoryId(Long catalogId) {
        List<PmsAttrGroup> pmsAttrGroups = baseMapper.selectList(new LambdaQueryWrapper<PmsAttrGroup>().eq(PmsAttrGroup::getCatalogId, catalogId));
        List<GroupWithAttrVO> groupWithAttrVOList = pmsAttrGroups.stream().map(pmsAttrGroup -> {
            GroupWithAttrVO groupWithAttrVO = new GroupWithAttrVO();
            BeanUtils.copyProperties(pmsAttrGroup, groupWithAttrVO);
            List<PmsAttrRequestVO> voList = pmsAttrService.attrGroupRelation(pmsAttrGroup.getAttrGroupId());
            groupWithAttrVO.setAttrs(voList);
            return groupWithAttrVO;
        }).collect(Collectors.toList());
        return groupWithAttrVOList;
    }
}




